package org.aplombh.java.leetcode.all;

public class _35搜索插入位置 {
    public static void main(String[] args) {
        System.out.println(new Solution35_1().searchInsert(new int[]{1, 3}, 2));
    }
}

class Solution35_1 {
    public int searchInsert(int[] nums, int target) {
        return binarySearch(nums, target, 0, nums.length-1);
    }

    public int binarySearch(int[] nums, int target, int left, int right) {
        if (left > right)
            return left;

        int mid = (left + right) >> 1;

        if (nums[mid] == target) return mid;
        if (nums[mid] > target)
            return binarySearch(nums, target, left, mid - 1);
        return binarySearch(nums, target, mid + 1, right);
    }
}